import message from '@/logic/message';
import loader from '@/logic/loader';
import runtimeManager from '@/logic/runtimeManager';
import callback from '@/logic/callback';

class MessageManager{
    constructor(){
        this.message=message;
    }
    init(){
        this.message.receive('loadResource',(msg)=>{
            // console.log("逻辑线程接收消息",msg)
            const {appId,bridgeId,pages,platform,currProjectPath}=msg
            loader.loadResource({appId,bridgeId,pages,platform,currProjectPath})
        })
        this.message.receive('createApp',(msg)=>{
            const {appId,bridgeId,pagePath,query,scene}=msg
            runtimeManager.createApp({appId,bridgeId,pagePath,query,scene});

            //向原生层发送消息
            message.send({
                type:'appIsCreated',
                body:{
                    bridgeId,
                }
            });
        })
        this.message.receive('createInstance',(msg)=>{
            console.log("createPage1")
            const {id,bridgeId,path,query}=msg
            runtimeManager.createPage({id,bridgeId,path,query});
        })
        this.message.receive('makeInitialData',(msg)=>{
            const {bridgeId,pagePath}=msg
            const initialData=loader.getInitialDataByPagePath(pagePath);
            //向原生层发送消息
            message.send({
                type:'initialDataIsReady',
                body:{
                    bridgeId,
                    initialData
                }
            });
        })
        //热更新
        this.message.receive('appShow',()=>{
            runtimeManager.appShow()
        })
        //热更新关才
        this.message.receive('appHide',()=>{
            runtimeManager.appHide()
        })
        this.message.receive('pageShow',(msg)=>{
            const {bridgeId}=msg
            runtimeManager.pageShow({id:bridgeId})
        })
        this.message.receive('pageHide',(msg)=>{
            const {bridgeId}=msg
            runtimeManager.pageHide({id:bridgeId})
        })
        this.message.receive('pageHide',(msg)=>{
            const {bridgeId}=msg
            runtimeManager.pageHide({id:bridgeId})
        })
        this.message.receive('pageUnload',(msg)=>{
            const {bridgeId}=msg
            runtimeManager.pageUnload({id:bridgeId})
        })
        this.message.receive('moduleMounted',(msg)=>{
            const {id}=msg
            runtimeManager.pageReady({id})
        })
        this.message.receive('pageScroll',(msg)=>{
            const {id,scrollTop}=msg
            runtimeManager.pageScroll({id,scrollTop})
        })
        this.message.receive('trrigerEvent',(msg)=>{
            const {id,methodName,param}=msg
            runtimeManager.trrigerEvent({id,methodName,param})
        })
        this.message.receive('trrigerCallback',(msg)=>{
            const {callbackId,arges}=msg
            callback.trrigerCallback({callbackId,arges})
        })
        this.message.receive('windowInfo',(msg)=>{
            const {windowWidth,windowHeight,statusBarHeight}=msg
            callback.getWindowInfo({windowWidth,windowHeight,statusBarHeight})
        })
        /* this.message.receive('getStorage',(msg)=>{
            const {result}=msg
            callback.getStorage({result})
        }) */
    }

}

export default new MessageManager();